package com.compton.UI
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/**
	 * This class enables sliding functionality to your slider assets. Calculates percentage of sliding and
	 * adds assets to be used. 
	 * @author blakecompton
	 * @example This is how to implement it into your Main class
	 * <listing version = "3.0">
	 *  var variable:Slider_Assets = new Slider_Assets();
	 * this.addChild(variable);
	 * 
	 * var newVariable:ScrollManager = new ScrollManager();
	 * newVariable.setUpAsstes(variable.mcTrack, variable.mcHandle);
	 * </listing>
	 */	
	
	public class SliderManager extends EventDispatcher
	{
		protected var _track:Sprite;
		protected var _handle:Sprite;
		protected var _percent:Number;
		
		public function SliderManager()
		{
			
		}

		/**
		 * setUpAssets sets the assets to be used. Throughing in parameters, such as, the track and the handle.
		 * There is a mouseEvent added to the function to add sliding capability to the handle. 
		 * @param track This parameter adds the track to the stage and provides neccessary functionality.
		 * @param handle This parameter adds the handle to the stage and provides neccessary functionality.
		 * 
		 * @example Implementation into the Main Class.
		 * <listing version = "3.0">
		 * 
		 * var slide2:Slider_Assets = new Slider_Assets();
		 * this.addChild(slide2);
		 * 
		 * var scroller:ScrollManager = new ScrollManager();
		 * scroller.setUpAsstes(slide2.mcTrack, slide2.mcHandle);
		 * </listing>
		 */		
		
		public function setUpAsstes(track:Sprite, handle:Sprite):void
		{
			_handle = handle;
			_track = track;
			
			handle.buttonMode = false;
			track.buttonMode = false;
			
			_handle.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
		}
		
		private function onMouseDown(evt:MouseEvent):void
		{
			_handle.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			
			_handle.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			
			var rect:Rectangle = new Rectangle(0,0, _track.width - _handle.width, 0);
			
			_handle.startDrag(false,rect);
		}
		
		private function onMouseUp(evt:MouseEvent):void
		{
			_handle.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			_handle.stopDrag();
		}
		
		
		
		private function onEnterFrame(evt:Event):void
		{
			var nEvent:Event = new Event(Event.CHANGE);
			dispatchEvent(nEvent);	
		}
		
		/**
		 * Getter and Setter provide the percent of the track that has been taken up. Sets the values in between 0 and 1 to create
		 * a percentage value. Percent is calculated by dividing the handles x position by the track width minus the handle width.
		 * @return
		 * @example _percent will set the percent that the slider is set too.
		 * <listing version="3.0">
		 * 
		 * _slider.percent = .3;
		 * 
		 * </listing>
		 */		
		
		public function get percent():Number
		{
			_percent = _handle.x/(_track.width - _handle.width)
			
			return _percent;
		}
		
		/**
		 * @private
		 */		
		
		public function set percent(value:Number):void
		{
			if(value < 0)
			{
				value = 0;
			}
			if(value > 1)
			{
				value = 1;
			}
			
			_percent = value;
			_handle.x = (_track.width - _handle.width) * _percent;	
		}
		
		/**
		 * Update handle method makes the percentage that you set, equal to the position of the slider.
		 */		
		
		protected function updateHandle():void
		{
			if(_handle && _track)
			{
				_handle.x = (_track.width - _handle.width) * _percent;
			}
		}
	}
}